/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 pjoc.pub, blademainer.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package pub.pjoc.common.util;

import com.xiongyingqi.util.StringUtil;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * A util generate order number.
 *
 * @author blademainer
 * @version 2016-09-16 16:05
 */
public abstract class OrderNumberGenerateUtils {
  public static final String DATE_FORMAT = "yyyyMMddHHmmssSSS";
  public static final int MACHINE_NUMBER_LENGTH = 10;
  // date+machineNumber+index
  private static final String PATTERN = "{0}{1}{2}";
  private static AtomicInteger index = new AtomicInteger();
  // Concurrent number in milliseconds
  private static int MAX_INDEX_IN_MILLISECONDS = 999999;
  private static int MAX_INDEX_LENGTH = (MAX_INDEX_IN_MILLISECONDS + "").length();


  public static String getDateString() {
    DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    String format = dateFormat.format(new Date());
    return format;
  }


  public static String getIndexString() {
    int next = index.getAndIncrement();
    if (next < 0) {
      next = -next;
    }
    int index = next % MAX_INDEX_IN_MILLISECONDS;
    return StringUtil.fillZero(index, MAX_INDEX_LENGTH);
  }

  public static String generateOrder() {
    String dateString = getDateString();
    String widthMachineNumber = HostUtils.getWidthMachineNumber(MACHINE_NUMBER_LENGTH);
    String indexString = getIndexString();
    String order = MessageFormat.format(PATTERN, dateString, widthMachineNumber, indexString);
    return order;
  }

  public static int getMaxIndexInMilliseconds() {
    return MAX_INDEX_IN_MILLISECONDS;
  }

  public static void setMaxIndexInMilliseconds(int maxIndexInMilliseconds) {
    MAX_INDEX_IN_MILLISECONDS = maxIndexInMilliseconds;
    MAX_INDEX_LENGTH = (MAX_INDEX_IN_MILLISECONDS + "").length();
  }
}
